#==============================================================================
# ■ Game_Battler (分割定义 1)
#------------------------------------------------------------------------------
# 　处理战斗者的类。这个类作为 Game_Actor 类与 Game_Enemy 类的
# 超级类来使用。
#==============================================================================

class Game_Battler
  #--------------------------------------------------------------------------
  # ● 定义实例变量
  #--------------------------------------------------------------------------
  attr_accessor :battler_name             # 战斗者 文件名
  attr_accessor :battler_hue              # 战斗者 色相
  attr_accessor :sp                       # SP
  attr_accessor :states                   # 状态
  attr_accessor :states_add               # 状态附加属性
  attr_accessor :cool_down                # 冷却
  attr_accessor :cd_turns                 # 冷却回合
  attr_accessor :hidden                   # 隐藏标志
  attr_accessor :immortal                 # 不死身标志
  attr_accessor :damage_pop               # 显示伤害标志
  attr_accessor :damage                   # 伤害值
  attr_accessor :fenshen                  # 影分身格挡概率
  attr_accessor :critical                 # 会心一击标志
  attr_accessor :animation_id             # 动画 ID
  attr_accessor :animation_hit            # 动画 击中标志
  attr_accessor :white_flash              # 白色屏幕闪烁标志
  attr_accessor :blink                    # 闪烁标志
  attr_accessor :name                     # 姓名
  attr_accessor :gender                   # 性别
  attr_accessor :age                      # 年龄
  attr_accessor :base_hit                 # 先天命中
  attr_accessor :base_eva                 # 先天闪避
  attr_accessor :base_atk                 # 先天攻击
  attr_accessor :base_def                 # 先天防御
  attr_accessor :fp_plus                  # 加力
  attr_accessor :mp_plus                  # 法点
  attr_accessor :morals                   # 道德
  attr_accessor :base_str                 # 先天膂力
  attr_accessor :base_agi                 # 先天敏捷
  attr_accessor :base_int                 # 先天悟性
  attr_accessor :base_bon                 # 先天根骨
  attr_accessor :base_fac                 # 先天外貌
  attr_accessor :base_luc                 # 福源
  attr_accessor :str_plus                 # 附加膂力
  attr_accessor :dex_plus                 # 附加速度
  attr_accessor :agi_plus                 # 附加敏捷
  attr_accessor :int_plus                 # 附加悟性
  attr_accessor :bon_plus                 # 附加根骨
  attr_accessor :hit_plus                 # 附加命中
  attr_accessor :eva_plus                 # 附加闪避
  attr_accessor :atk_plus                 # 附加攻击
  attr_accessor :def_plus                 # 附加防御
  attr_accessor :hp                       # 生命
  attr_accessor :maxhp                    # 生命上限
  attr_accessor :fp                       # 内力
  attr_accessor :maxfp                    # 内力上限
  attr_accessor :mp                       # 法力
  attr_accessor :maxmp                    # 法力上限
  attr_accessor :full_hp                  # 生命满上限
  attr_accessor :gold                     # 金钱
  attr_accessor :exp                      # 经验
  attr_accessor :weapon_id                # 武器ID
  attr_accessor :skill_use                # 装备技能
  attr_accessor :skill_count              # 技能数量
  attr_accessor :skill_list               # 技能列表
  attr_accessor :kf_ap                    # 招式命中参数
  attr_accessor :kf_dp                    # 招式闪避参数
  attr_accessor :kf_pp                    # 招式招架参数
  #--------------------------------------------------------------------------
  # ● 初始化对像
  #--------------------------------------------------------------------------
  def initialize
    @battler_name,@battler_hue,@hp,@sp,@states,@states_turn= "",0,0,0,[],{}
    @maxhp_plus,@maxsp_plus,@str_plus,@dex_plus,@agi_plus = 0,0,0,0,0
    @int_plus,@bon_plus,@hit_plus,@eva_plus,@atk_plus = 0,0,0,0,0
    @def_plus,@luc_plus,@hidden,@immortal,@damage_pop = 0,0,false,false,false
    @damage,@critical,@animation_id,@animation_hit = nil,false,0,false
    @white_flash,@blink,@cool_down,@cd_turns = false,false,[],{}
    @kf_ap,@kf_dp,@kf_pp = 0,0,0
    @gender,@age,@base_hit,@base_eva,@base_atk,@base_def = 0,0,0,0,0,0
    @fp_plus,@mp_plus,@morals,@base_str,@base_agi,@fenshen = 0,0,0,0,0,-1
    @base_int,@base_bon,@base_fac,@base_luc,@hp,@fp = 0,0,0,0,0,0
    @maxfp,@mp,@maxmp,@full_hp,@gold,@exp,@states_add = 0,0,0,0,0,0,{}
    @weapon_id,@skill_use,@skill_count,@skill_list= 0,[0,0,0,0,0,0],0,[]
  end
  #--------------------------------------------------------------------------
  # ● 获取基础属性数组
  #--------------------------------------------------------------------------
  def base_ability
    return [@base_str,@base_agi,@base_int,@base_bon]
  end
  #--------------------------------------------------------------------------
  # ● 获取膂力
  #--------------------------------------------------------------------------
  def str
    n = @base_str+get_kf_level(2)/10+equip_str+@str_plus
    n = [n,255].min
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取敏捷
  #--------------------------------------------------------------------------
  def agi
    n = @base_agi+get_kf_level(9)/10+equip_agi+@agi_plus
    n = [n,255].min
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取根骨
  #--------------------------------------------------------------------------
  def bon
    n = @base_bon+get_kf_level(1)/10+equip_bon+@bon_plus
    n = [n,255].min
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取悟性
  #--------------------------------------------------------------------------
  def int
    n = @base_int+get_kf_level(11)/10+equip_int+@int_plus
    n = [n,255].min
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取外貌
  #--------------------------------------------------------------------------
  def face
    n = @base_fac+get_kf_level(22)/10+equip_fac
    n = [n,255].min
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取福源
  #--------------------------------------------------------------------------
  def luck
    return @base_luc
  end
  #--------------------------------------------------------------------------
  # ● 获取指定技能位置序号
  #--------------------------------------------------------------------------
  def get_kf_index(id)
    # 技能列表为空
    return -1 if @skill_list.size == 0
    n = -1
    @skill_list.each_index do |i|
      kf_data = @skill_list[i]
      if id == kf_data[0]
        n = i
        break
      end
    end
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取攻击技能ID
  #--------------------------------------------------------------------------
  def attack_kf_id
    return @weapon_id > 0 ? weapon_kf_id : hand_kf_id
  end
  #--------------------------------------------------------------------------
  # ● 获取使用拳脚ID
  #--------------------------------------------------------------------------
  def hand_kf_id
    n = @skill_use[0]
    n = 2 if n == 0
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取使用兵刃ID
  #--------------------------------------------------------------------------
  def weapon_kf_id
    return 0 if @weapon_id == 0
    n = @skill_use[1]
    n = weapon_basic_kf if not weapon_match_kf?
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取使用轻功ID
  #--------------------------------------------------------------------------
  def dodge_kf_id
    n = @skill_use[2]
    n = 9 if n == 0
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取使用内功ID
  #--------------------------------------------------------------------------
  def fp_kf_id
    n = @skill_use[3]
    n = 1 if n == 0
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取使用法术ID
  #--------------------------------------------------------------------------
  def mp_kf_id
    n = @skill_use[5]
    n = 8 if n == 0
    return n
  end
  #--------------------------------------------------------------------------
  # ● 武器对应基本功夫ID
  #--------------------------------------------------------------------------
  def weapon_basic_kf
    return ($data_weapons[@weapon_id].type+3)
  end
  #--------------------------------------------------------------------------
  # ● 武器是否与兵刃功夫匹配
  #--------------------------------------------------------------------------
  def weapon_match_kf?
    return false if @skill_use[1] == 0
    n = $data_kungfus[@skill_use[1]].type - 3
    return ($data_weapons[@weapon_id].type == n)
  end
  #--------------------------------------------------------------------------
  # ● 招架是否与攻击功夫匹配
  #--------------------------------------------------------------------------
  def parry_match_atk?
    if @weapon_id>0
      n = 1
      return false if not weapon_match_kf?
    else
      n = 0
    end
    return (@skill_use[n]==@skill_use[4])
  end
  #--------------------------------------------------------------------------
  # ● 获取指定ID功夫对应基本功夫ID
  #--------------------------------------------------------------------------
  def get_basic_id(id)
    return 0 if id == 0
    return $data_kungfus[id].type
  end
  #--------------------------------------------------------------------------
  # ● 获取最高功夫等级
  #--------------------------------------------------------------------------
  def get_max_level
    return 0 if @skill_list.size == 0
    n = 0
    @skill_list.each do |i|
      n = i[1] if i[1] > n
    end
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取指定ID功夫等级
  #--------------------------------------------------------------------------
  def get_kf_level(id)
    return 0 if id == 0
    n = get_kf_index(id)
    return 0 if n == -1
    return @skill_list[n][1]
  end
  #--------------------------------------------------------------------------
  # ● 获取出手等级
  #--------------------------------------------------------------------------
  def atk_level
    n = str + atk + @fp_plus/2
    return [n/20,5].min
  end
  #--------------------------------------------------------------------------
  # ● 获取指定ID功夫对应有效等级（基础功夫/2+门派功夫）
  #--------------------------------------------------------------------------
  def get_kf_efflv(id)
    n = 0
    # 基本功夫等级/2
    n += get_kf_level(get_basic_id(id))/2
    # 门派功夫等级
    n += get_kf_level(id) if id >= 12
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取攻击功夫有效等级
  #--------------------------------------------------------------------------
  def attack_kf_lv
    n = 0
    # 空手则+拳脚有效等级
    if @weapon_id == 0 
      n += hand_kf_lv
    else
      # 装备武器的情况+兵刃有效等级
      n += weapon_kf_lv
    end
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取拳脚功夫有效等级
  #--------------------------------------------------------------------------
  def hand_kf_lv
    return get_kf_efflv(hand_kf_id)
  end
  #--------------------------------------------------------------------------
  # ● 获取兵刃功夫有效等级
  #--------------------------------------------------------------------------
  def weapon_kf_lv
    if weapon_match_kf?
      # 武器匹配功夫则返回兵刃功夫有效等级
      return get_kf_efflv(weapon_kf_id)
    else # 不匹配返回武器对应基本兵刃等级/2
      return get_kf_level(weapon_basic_kf)/2
    end
  end
  #--------------------------------------------------------------------------
  # ● 获取轻功功夫有效等级
  #--------------------------------------------------------------------------
  def dodge_kf_lv
    return get_kf_efflv(dodge_kf_id)
  end
  #--------------------------------------------------------------------------
  # ● 获取招架功夫有效等级
  #--------------------------------------------------------------------------
  def parry_kf_lv
    n = 0
    skill = @weapon_id>0 ? @skill_use[1] : @skill_use[0]
    # +基本招架等级/2
    n += get_kf_level(10)/2
    # 空手的情况
    if @weapon_id == 0
      # 招架与拳脚匹配则+使用拳脚等级
      n += parry_match_atk? ? get_kf_level(skill) : 0
    else
      # 装备武器的情况
      # 武器与功夫匹配且招架与功夫匹配则+兵刃等级
      n += (parry_match_atk? and weapon_match_kf?) ? get_kf_level(skill) : 0
    end
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取内功功夫有效等级
  #--------------------------------------------------------------------------
  def fp_kf_lv
    return get_kf_efflv(fp_kf_id)
  end
  #--------------------------------------------------------------------------
  # ● 获取法术功夫有效等级
  #--------------------------------------------------------------------------
  def mp_kf_lv
    return get_kf_efflv(mp_kf_id)
  end
  #--------------------------------------------------------------------------
  # ● 获取等级
  #--------------------------------------------------------------------------
  def level
    n = (attack_kf_lv+(dodge_kf_lv+parry_kf_lv)/2)/3
    n = [n/5,49].min
    return n
  end
  #--------------------------------------------------------------------------
  # ● 获取命中
  #--------------------------------------------------------------------------
  def hit
    return @base_hit+equip_hit+@hit_plus
  end
  #--------------------------------------------------------------------------
  # ● 获取攻击力
  #--------------------------------------------------------------------------
  def atk
    return @base_atk+equip_atk+@atk_plus
  end
  #--------------------------------------------------------------------------
  # ● 获取防御
  #--------------------------------------------------------------------------
  def pdef
    return @base_def+equip_def+@def_plus
  end
  #--------------------------------------------------------------------------
  # ● 获取闪避
  #--------------------------------------------------------------------------
  def eva
    return @base_eva+equip_eva+@eva_plus
  end
  #--------------------------------------------------------------------------
  # ● 获取绝招
  #--------------------------------------------------------------------------
  def skills
    n = []
    # 装备武器则获取兵刃功夫的绝招
    if @weapon_id > 0
      n += $data_kungfus[weapon_kf_id].skill
    else
      n += $data_kungfus[hand_kf_id].skill
    end
    # 获取轻功绝招
    n += $data_kungfus[dodge_kf_id].skill
    # 获取内功绝招
    n += $data_kungfus[fp_kf_id].skill
    # 获取法术绝招
    n += $data_kungfus[mp_kf_id].skill
    n.delete(0)
    return n
  end
  #--------------------------------------------------------------------------
  # ● 更改 HP
  #     hp : 新的 HP
  #--------------------------------------------------------------------------
  def hp=(hp)
    @hp = [[hp, @maxhp].min, 0].max
  end
  #--------------------------------------------------------------------------
  # ● 更改 FP
  #     fp : 新的 FP
  #--------------------------------------------------------------------------
  def fp=(fp)
    @fp = [fp, 0].max
  end
  #--------------------------------------------------------------------------
  # ● 更改 MP
  #     mp : 新的 MP
  #--------------------------------------------------------------------------
  def mp=(mp)
    @mp = [mp, 0].max
  end
  #--------------------------------------------------------------------------
  # ● 获取当前的动作
  #--------------------------------------------------------------------------
  def current_action
    return @current_action
  end
  #--------------------------------------------------------------------------
  # ● 确定动作速度
  #--------------------------------------------------------------------------
  def make_action_speed
    @current_action.speed = agi + rand(10 + agi / 4)
  end
  #--------------------------------------------------------------------------
  # ● 战斗不能判定
  #--------------------------------------------------------------------------
  def dead?
    return (@hp == 0 and not @immortal)
  end
  #--------------------------------------------------------------------------
  # ● 存在判定
  #--------------------------------------------------------------------------
  def exist?
    return (not @hidden and (@hp > 0 or @immortal))
  end
  #--------------------------------------------------------------------------
  # ● HP 0 判定
  #--------------------------------------------------------------------------
  def hp0?
    return (not @hidden and @hp == 0)
  end
  #--------------------------------------------------------------------------
  # ● 可以输入命令判定
  #--------------------------------------------------------------------------
  def inputable?
    return (not @hidden and restriction <= 1)
  end
  #--------------------------------------------------------------------------
  # ● 可以行动判定
  #--------------------------------------------------------------------------
  def movable?
    return (not state?(0))
  end
  #--------------------------------------------------------------------------
  # ● 防御中判定
  #--------------------------------------------------------------------------
  def guarding?
    return (@current_action.kind == 0 and @current_action.basic == 1)
  end
  #--------------------------------------------------------------------------
  # ● 休止中判定
  #--------------------------------------------------------------------------
  def resting?
    return (@current_action.kind == 0 and @current_action.basic == 3)
  end
end
